layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate', '$http', 'zmnUtils'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var laydate = layui.laydate;
    var $http = layui.$http;
    var zmnUtils = layui.zmnUtils;

    var configObj = json.obj;

    // 点击取消
    $("button#cancel").click(function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);//关闭当前页
    });

    form.verify({
        percentVal: function (value, item) {
            if (value !== "") {
                if (Number(value) < 0) {
                    return '占比数值不能小于0';
                }
                if (Number(value) > 100) {
                    return '占比数值不能大于100';
                }
            }
        },
        reworkLimit: function (value, item) {
            if (value !== "" && value !== '0') {
                if (Number(value) < 0) {
                    return '最大返修单量不能小于0';
                }
                if (Number(value) > 1000000) {
                    return '最大返修单量不能大于1000000';
                }
                var r = /^[1-9]\d*$/;
                if (!r.test(value)) {
                    return '只能输入正整数，请重新输入';
                }
            }
        }
    });

    // 是否为正整数
    function isPositiveInteger(number) {
        var re = /^[0-9]\d*$/;
        return re.test(number)
    }

    // 比较两个时间的先后
    function compareDate(t1, t2) {
        var date = new Date();
        var a = t1.split(":");
        if (a[0] < 0 || a[0] > 23) {
            return true;
        }
        if (a[1] < 0 || a[1] > 59) {
            return true;
        }
        var b = t2.split(":");
        if (b[0] < 0 || b[0] > 23) {
            return true;
        }
        if (b[1] < 0 || b[1] > 59) {
            return true;
        }
        return date.setHours(a[0], a[1]) > date.setHours(b[0], b[1]);
    }

    // 保存服务商信息
    form.on('submit(zmn-form-submit)', function (obj) {
        var data = obj.field;
        var url = "/orderconfig/distribute/company/modify";

        // 时间格式校验
        let autoTimeStr = data.autoTimeStr;
        let timeArr = autoTimeStr.split(' ~ ');
        if (compareDate(timeArr[0], timeArr[1])) {
            layer.msg('时间格式不正确!');
            return false;
        }

        $http.save(url, data).then(function () {
            zmnUtils.layui.toastSuccess('成功', function () {
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
                window.parent.location.reload();
            })
        }, function (msg) {
            zmnUtils.layui.toastError(msg);
        });
        return false;
    });

    // 区域下拉框联动
    zmnPage.laySelectLinkage(form, "/orderconfig/distribute/listCompanyCity.action", "companyId", "cityId");

    var timeStr = configObj.autoTimeStart == null ? '07:00 ~ 21:00' : (configObj.autoTimeStart + " ~ " + configObj.autoTimeEnd);

    //日期时间范围选择
    laydate.render({
        elem: '#autoTimeStr'
        , type: 'time'
        , range: '~'
        , format: 'HH:mm'
        , value: timeStr
        , trigger: 'click'
    });

    table.init('zmn-table', {
        page: false
    });

    table.on('edit(zmn-table)', function (obj) {
        let value = obj.value; // 修改后的值
        let field = obj.field; //得到字段
        configObj[field] = value;

        console.log(value + " - " + field);
    });

    //对外暴露的接口
    exports('orderconfig_distribute_f_company_edit', {});
});